package ru.pb.game.network.engine.client.packets.receivable;

import ru.pb.game.network.engine.client.packets.FromClientToGameServerPacket;
import ru.pb.game.network.engine.client.packets.sendable.PROTOCOL_BATTLE_PRESTARTBATTLE_ACK;
import ru.pb.game.network.engine.client.packets.sendable.opcode_3592_ACK;
import ru.pb.game.objects.model.players.Player;
import ru.pb.game.objects.model.players.enums.SLOT_STATE;
import ru.pb.game.objects.model.room.Room;
import ru.pb.game.objects.storage.ObjectsStorage;

public class PROTOCOL_BATTLE_PRESTARTBATTLE_REQ extends FromClientToGameServerPacket
{

	public PROTOCOL_BATTLE_PRESTARTBATTLE_REQ()
	{}

	@Override
	public void readImpl()
	{

	}

	@Override
	public void runImpl()
	{
		Player player = getPlayer();
		if(player != null)
		{
			Room room = player.getRoom();
			if(room != null)
			{
				room.changeSlotState(player.getSlot(), SLOT_STATE.SLOT_STATE_PRESTART);
				if(player.isRoomLeader())
				{
					Player member = null;
					for(int objId : room.getAllPlayers().toArray())
					{
						member = ObjectsStorage.getInstance().getPlayer(objId);
						// отправляем хосту инфу об всех игроках в комнате
						player.sendPacket(new PROTOCOL_BATTLE_PRESTARTBATTLE_ACK(member, 1));
					}
				}
				else
				{
					// если игрок не является хостом то отправляем как обычно
					player.sendPacket(new PROTOCOL_BATTLE_PRESTARTBATTLE_ACK(room.getLeader(), 1));
				}
				player.sendPacket(new opcode_3592_ACK(room));
			}
		}
	}
}